import api from "@/api/domain"
import { Message } from 'iview'

export default {
    state: { 
        domainList: {
            list: [],
            page_size: 20,
            total: 0
        }, 
        allCustomers: []
    },
    getters: { 
        domainList: state => state.domainList, 
        allCustomers: state => state.allCustomers, 
    },
    actions: { 
        fetchDomainList({commit}, condition){ 
            api.fetchDomainList(condition).then(reply => { 
                if(reply.data.ok){
                    commit('FETCH_DOMAIN_LIST', reply.data);  
                }else{
                    Message.error(reply.data.msg || "请求失败")
                } 
            })
        },
        createDomain({commit}, condition){ 
            api.createDomain(condition.params).then(reply => {  
                condition.cb && condition.cb(reply.data)
            })
        },
        deleteDomain({commit}, condition){ 
            api.deleteDomain(condition).then(reply => {   
                condition.cb && condition.cb(reply.data)
            })
        },
        modifyDomain({commit}, condition){ 
            api.modifyDomain(condition.params).then(reply => {   
                condition.cb && condition.cb(reply.data)
            })
        },
        fetchAllCustomerList({commit}){
            api.fetchAllCustomerList().then(reply => { 
                if(reply.data.ok){
                    commit('FETCH_ALL_CUSTOMER_LIST', reply.data.data); 
                }else{
                    Message.error(reply.data.msg || "请求失败")
                } 
            }) 
        }
        
    },
    mutations: {
        FETCH_ALL_CUSTOMER_LIST(state, data){
            state.allCustomers = data
        },
        FETCH_DOMAIN_LIST(state, data){ 
            state.domainList.list = data.data 
            state.domainList.page_size = data.paginator.PageSize 
            state.domainList.total = data.paginator.total 
        }, 
    }
}